package com.itheima.health.service;

import com.itheima.health.dto.SetMealDto;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐模块管理")
public class SetMealController {
    @Reference
    private SetmealService setmealService;
    @PostMapping("setmeal/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file){
        try {
            //获取源文件名
            String originalFilename = file.getOriginalFilename();
            //随机uuid文件名
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
            //上传到阿里云
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
            //保存到set集合
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
            //存储到redis
            RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfilename,uuidfilename,2,TimeUnit.MINUTES);
            //返回
            return new Result(uuidfilename);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    //分页查询
    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询",notes = "根据客户端请求，完成分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前分页页码",required=true,paramType="form",dataType="Integer"),
            @ApiImplicitParam(name="pageSize",value="每页显示记录数",required=true,paramType="form",dataType="Integer"),
            @ApiImplicitParam(name="queryString",value="查询条件",required=false,paramType="form",dataType="String")
    })
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        //System.out.println(queryPageBean);
        //将分页得到的数据放到Result中
        return new Result(setmealService.findPage(queryPageBean));
    }

    //添加套餐
    @PostMapping("setmeal/add")
    public Result add(@RequestBody SetMealDto setMealDto){
        //套餐添加的时候将图片从set集合清除
        setmealService.saveSetmeal(setMealDto);
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setMealDto.getImg());
        // 同时判断redis中的图片是否过期,如果过期，redis会自动清除，此时删除阿里云上对应的过期图片
        RedisUtil.delete(RedisConstant.SINGLE_PIC+setMealDto.getImg());
      return new Result(setmealService.saveSetmeal(setMealDto));

    }

    //编辑表单数据回显   setmeal/updatesetmealById
    @GetMapping("setmeal/updatesetmealById/{id}")
    public Result updatesetmealById(@PathVariable("id") Integer id ){
        return new Result(setmealService.updatesetmealById(id));
    }



//    //逻辑删除数据(根据套餐的id进行删除)
//    @DeleteMapping("setmeal/deleteSetmealById/{id}")
//    public Result deletePageById(@PathVariable("id") int id){
//        return new Result(setmealService.updateSetmealById(id));
//    }

}
